package com.letou.ls.view;

import java.util.ArrayList;

import com.letou.ls.uitl.Logger;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

public class TableBadyLineView2 extends View {

	private ArrayList<ArrayList<ArrayList<String>>> mDataList;
	private int tableTextColor = Color.BLACK;
	private int tableBackgroundAColor = Color.WHITE;
	private int tableBackgroundCColor = Color.parseColor("#ccffff");
	private int tableBackgroundDColor = Color.parseColor("#cacdd5");
	private ArrayList<Float> mWidthList;

	/**
	 * 位图变量。
	 */
	public Bitmap mBitmap;
	/**
	 * 画布变量。
	 */
	// private Canvas mCanvas;
	/**
	 * 图片控制矩阵。
	 */
	private Matrix mMatrix = new Matrix();
	public float mWidth;
	public float mHeight;
	private Paint mPaint;
	private Paint mBgPaint;
	private float textSize;
	private float itemHeight;
	private ArrayList<Integer> lineOne;
	private ArrayList<Integer> lineTwo;
	private ArrayList<Integer> lineThree;
	private int startOne;
	private int startTwo;
	private int startThree;
	private Paint mLinePaint;

	public TableBadyLineView2(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mPaint = new Paint();
		/* 去锯齿 */
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.BLACK);

		mBgPaint = new Paint();
		/* 去锯齿 */
		mBgPaint.setAntiAlias(true);
		mBgPaint.setColor(Color.BLACK);

		mLinePaint = new Paint();
		/* 去锯齿 */
		mLinePaint.setAntiAlias(true);
		mLinePaint.setStrokeWidth(3);
		mLinePaint.setColor(Color.BLUE);

	}

	public TableBadyLineView2(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TableBadyLineView2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
	}

	public void startLine(int startOne, int startTwo, int startThree) {
		this.startOne = startOne;
		this.startTwo = startTwo;
		this.startThree = startThree;
	}

	public void addLine(ArrayList<Integer> oneLine, ArrayList<Integer> twoLine, ArrayList<Integer> threeLine) {
		lineOne = oneLine;
		lineTwo = twoLine;
		lineThree = threeLine;

		if (oneLine == null) {
			Logger.v("main", "oneLine==null");
		} else {
			Logger.v("main", "oneLine " + oneLine.size());
		}
	}

	public void updateData(ArrayList<ArrayList<ArrayList<String>>> datalist, ArrayList<Float> widthList, float textSize,
			float itemHeight) {
		this.setBackgroundColor(Color.RED);

		mDataList = datalist;
		mWidthList = widthList;
		mPaint.setTextSize(textSize);
		this.textSize = textSize;
		this.itemHeight = itemHeight;
		reDarwBitmap();
		invalidate();
	}

	public ArrayList<ArrayList<ArrayList<String>>> get_data() {
		return mDataList;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
		mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
		setMeasuredDimension((int) mWidth, (int) mHeight);

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Logger.v("main", "onDraw   " + mWidth + "  " + mHeight);
		if (mBitmap == null) {
			reDarwBitmap();
		}
		if (mBitmap != null) {
			canvas.drawBitmap(mBitmap, mMatrix, null);
		}
	}

	private void reDarwBitmap() {
		if (mBitmap != null) {
			mBitmap.recycle();
		}
		if (mHeight <= 0) {
			return;
		}
		mBitmap = Bitmap.createBitmap((int) mWidth, (int) mHeight, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(mBitmap);
		canvas.drawBitmap(mBitmap, mMatrix, null);

		/**
		 * 画表内容
		 */
		if (mDataList != null) {
			int size = mDataList.size();
			// int size = 1;
			float startY = 1;
			float[] widthArray = null;
			for (int i = 0; i < size; i++) {
				int ss = mDataList.get(i).size();
				float startX = 0;

				if (widthArray == null) {
					widthArray = new float[ss];
				}
				for (int j = 0; j < ss; j++) {

					String label = mDataList.get(i).get(j).get(0);
					float width;
					if (j < mWidthList.size()) {
						width = mWidthList.get(j).intValue();
					} else {
						width = 50;
					}

					String type = null;
					if (mDataList.get(i).get(j).size() == 2) {
						type = mDataList.get(i).get(j).get(1);
					}

					if ("1".equals(type)) {
						mPaint.setColor(Color.RED);

						mBgPaint.setColor(tableBackgroundAColor);
					} else if ("2".equals(type)) {

						mPaint.setColor(Color.RED);
						mBgPaint.setColor(tableBackgroundCColor);
					} else if ("3".equals(type)) {

						mPaint.setColor(tableTextColor);
						mBgPaint.setColor(tableBackgroundCColor);
					} else if ("4".equals(type)) {

						mPaint.setColor(tableTextColor);
						mBgPaint.setColor(tableBackgroundDColor);
					} else if ("5".equals(type)) {

						mPaint.setColor(Color.RED);
						mBgPaint.setColor(tableBackgroundDColor);
					} else {
						mPaint.setColor(tableTextColor);
						mBgPaint.setColor(tableBackgroundAColor);
					}

					float x = startX;
					float y = startY;
					startX += width + 1;
					widthArray[j] = startX;

					canvas.drawRect(x, y, x + width - 1, y + itemHeight, mBgPaint);

					if (!TextUtils.isEmpty(label)) {

						float textWidth = mPaint.measureText(label);
						x += (width - textWidth) / 2f;
						canvas.drawText(label, x, y + (textSize + itemHeight) / 2f, mPaint);

					}

				}

				if (i != 0) {
					drawLine(i, startOne, startY, lineOne, widthArray, canvas);
					drawLine(i, startTwo, startY, lineTwo, widthArray, canvas);
					drawLine(i, startThree, startY, lineThree, widthArray, canvas);
				}
				
				
				

				startY += itemHeight + 1;
			}
		}

	}

	private void drawLine(int i, int startNum, float startY, ArrayList<Integer> lineArray, float[] widthArray,
			Canvas canvas) {

		if (lineArray == null && i >= lineArray.size()) {
			return;
		}
		int beforePoint = lineArray.get(i - 1) + startNum - 1;
		int end = lineArray.get(i) + startNum - 1;

		float beginX = widthArray[beforePoint - 1] + mWidthList.get(beforePoint).intValue() / 2f;
		float beginY = startY - itemHeight / 2f;
		float endX = widthArray[end - 1] + mWidthList.get(end).intValue() / 2f;
		float endY = startY + itemHeight / 2f;

		canvas.drawLine(beginX, beginY, endX, endY, mLinePaint);
	}
}
